﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ITAP.ShprossesBuilder.Modelling.Components.Constraints
{
    [Serializable]
    public class RemoveInnerStickConnectorsConstraint : IConstraint
    {
        private bool enabled = true;

        public RemoveInnerStickConnectorsConstraint()
        {
        }

        #region IConstraint Members

        public bool Check(IComponent component)
        {
            List<IComponent> componentsToRemove = new List<IComponent>();
            foreach(IComponent innerComponent in component.InnerComponents)
                if (innerComponent.GetType() == typeof(StickConnector))
                {
                    StickConnector connector = ((StickConnector)innerComponent);

                    //if(!(connector.StartConnectedComponent.IsConnected(connector) &&
                    //    connector.IsConnected(connector.StartConnectedComponent)))
                    //{
                    //    return false;
                    //}

                    //if (!(connector.EndConnectedComponent.IsConnected(connector) &&
                    //    connector.IsConnected(connector.EndConnectedComponent)))
                    //{
                    //    return false;
                    //}

                    if (connector.StartConnectedComponent == null || connector.EndConnectedComponent == null)
                        return false;
                }
            return true;
        }

        public void Fix(IComponent component)
        {
            if (!enabled)
                return;

            List<IComponent> componentsToRemove = new List<IComponent>();
            foreach (IComponent innerComponent in component.InnerComponents)
                if (innerComponent.GetType() == typeof(StickConnector))
                {
                    StickConnector connector = ((StickConnector)innerComponent);

                    //if (!(connector.StartConnectedComponent.IsConnected(connector) &&
                    //    connector.IsConnected(connector.StartConnectedComponent)))
                    //{
                    //    componentsToRemove.Add(innerComponent);
                    //    continue;
                    //}

                    //if (!(connector.EndConnectedComponent.IsConnected(connector) &&
                    //    connector.IsConnected(connector.EndConnectedComponent)))
                    //{
                    //    componentsToRemove.Add(innerComponent);
                    //    continue;
                    //}

                    if (connector.StartConnectedComponent == null || connector.EndConnectedComponent == null)
                    {
                        componentsToRemove.Add(innerComponent);
                      //  continue;
                    }
                    
                }
            foreach (IComponent innerComponent in componentsToRemove)
                component.RemoveInnerComponent(innerComponent);
        }

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        public ConstraintAction Action
        {
            get { return ConstraintAction.Fix; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            RemoveInnerStickConnectorsConstraint clone = new RemoveInnerStickConnectorsConstraint();
            clone.enabled = this.enabled;
            return clone;
        }

        #endregion
    }
}
